<?php
class Helper_CurrentDate
{
    static protected $_date;
    static protected $_year;
    static protected $_month;
    static protected $_day;

    /**
     * Get current day
     *
     * @return string
     */
    public function getCurrentDay()
    {
        if (null === self::$_date) {
            $this->setCurrentDate($this->_getCurrentDateFromRequest());
        }
        return self::$_day;
    }

    /**
     * Get current month
     *
     * @return string
     */
    public function getCurrentMonth()
    {
        if (null === self::$_date) {
            $this->setCurrentDate($this->_getCurrentDateFromRequest());
        }
        return self::$_month;
    }

    /**
     * Get current year
     *
     * @return string
     */
    public function getCurrentYear()
    {
        if (null === self::$_date) {
            $this->setCurrentDate($this->_getCurrentDateFromRequest());
        }
        return self::$_year;
    }

    /**
     * Get current date
     *
     * @return string
     */
    public function getCurrentDate()
    {
        if (null === self::$_date) {
            $this->setCurrentDate($this->_getCurrentDateFromRequest());
        }
        return self::$_date;
    }

    /**
     * Set current date
     *
     * @return string
     */
    public function setCurrentDate($date)
    {
        self::$_date = $date;
        return $this;
    }

    /**
     * Get current date from request
     *
     * @return string
     */
    protected function _getCurrentDateFromRequest()
    {
        $year   = (int) isset($_GET['year'])  ? $_GET['year']  : date('Y');
        $month  = (int) isset($_GET['month']) ? $_GET['month'] : date('m');
        $day    = (int) isset($_GET['day'])   ? $_GET['day']   : date('d');

        return $this->_formatDate($year, $month, $day);
    }

    /**
     * Format date
     *
     * @param  $year
     * @param  $month
     * @param  $day
     * @return string
     */
    protected function _formatDate($year, $month, $day)
    {
        $day    = strlen($day) < 2   ? "0$day"   : $day;
        $month  = strlen($month) < 2 ? "0$month" : $month;
        $this->_setSeparatedDate($year, $month, $day);
        return "$year-$month-$day";
    }

    /**
     * Set separated date
     *
     * @param  $year
     * @param  $month
     * @param  $day
     * @return Helper_CurrentDate
     */
    protected function _setSeparatedDate($year, $month, $day)
    {
        self::$_year  = $year;
        self::$_month = $month;
        self::$_day   = $day;
        return $this;
    }

    /**
     * Get query string
     *
     * @return string
     */
    public function getQueryString()
    {
        $query = '';
        if ($this->getCurrentYear() != date('Y')) {
            $query .= '&year='   . $this->getCurrentYear();
        }
        if ($this->getCurrentMonth() != date('m')) {
            $query .= '&month='   . $this->getCurrentMonth();
        }
        if ($this->getCurrentDay() != date('d')) {
            $query .= '&day='   . $this->getCurrentDay();
        }
        return $query;
    }
}

